﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Master_DocContractSrhForm
 * 类型：       控制层
 * 功能说明：   销售资料维护检索页面
 * 作者：       章 昕
 * 创建日期：   2007/08/20
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Master.Dao;
using log4net;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Master.Bean;
using Shpm.App.Commons.Utils;
using Shpm.System;

public partial class App_Master_DocContractSrhForm : System.Web.UI.Page
{
    protected static ILog log = LogManager.GetLogger(typeof(App_Master_DocContractSrhForm));

    // 检索按钮 click 标志    
    private const string COMMAND_SEARCH = "DoSearch";

    // PAGE LOAD 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // init bind controls
            lstDocType.Items.Clear();
            lstDocType.Items.Add("");
            bindCommonDicCats();

            if (Context.Items["PAGE_BACK"] == null)
            {
                viewSrhRst.DataSourceID = null;
            }
            else 
            {
                if (Context.Items["docType"] != null &&
                    Context.Items["docTitle"] != null &&
                    Context.Items["docContent"] != null &&
                    Context.Items["docIdSrh"] != null)
                {
                    this.lstDocType.SelectedValue = Context.Items["docType"].ToString();
                    this.txtDocTitle.Text = Context.Items["docTitle"].ToString();
                    this.txtDocKeyword.Text = Context.Items["docContent"].ToString();
                    this.txtDocNo.Text = Context.Items["docIdSrh"].ToString();
                    this.hidDocType.Value = this.lstDocType.SelectedValue;
                    this.hidDocId.Value = this.txtDocNo.Text;
                    this.hidDocTitle.Value = this.txtDocTitle.Text;
                    this.hidDocContent.Value = this.txtDocKeyword.Text;
                    this.viewSrhRst.DataSourceID = this.ObjectDataSource1.ID;
                    // 接收传递参数为分页编号
                    if (Context.Items.Contains("ParamPageIndex"))
                    {
                        viewSrhRst.PageIndex = Convert.ToInt32(Context.Items["ParamPageIndex"]);
                    }
                }
            }                        
        }

    }

    // 检索按钮click
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (comValidatSrh.DoValidator())
        {
            //this.ViewState["SHOW_SRH_LST"] = "1";
            this.hidDocType.Value = this.lstDocType.SelectedValue;
            this.hidDocId.Value = this.txtDocNo.Text;
            this.hidDocTitle.Value = this.txtDocTitle.Text;
            this.hidDocContent.Value = this.txtDocKeyword.Text;
            this.viewSrhRst.DataSourceID = this.ObjectDataSource1.ID;
            viewSrhRst.PageIndex = 0;
            ViewState["PAGE_COMMAND"] = COMMAND_SEARCH;
        }
        else
        {
            ((Design_MasterPage)this.Master).InfoMessage = comValidatSrh.ErrorMessage;                
        }
    }

    // 新建按钮click
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Context.Items["docType"] = this.hidDocType.Value;
        Context.Items["docTitle"] = this.hidDocTitle.Value;
        Context.Items["docContent"] = this.hidDocContent.Value;
        Context.Items["docIdSrh"] = this.hidDocId.Value;
        // 传递分页编号
        Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;

        // 页面跳转至更新画面
        // Server.Transfer("~/App/Master/DocContractUpdForm.aspx", true);
        ControllerUtils.Transfer(this.Context, "~/App/Master/DocContractUpdForm.aspx", true);
    }

    // bind common catalog data  
    private void bindCommonDicCats()
    {
        DocContractDao dao = new DocContractDao();
        CommonDictionaryBean beanComDict = new CommonDictionaryBean();
        IList lstCat = null;

        // 资料类别
        log.Fatal("\n ***** get SALE_DOC_TYPE catalog *****");
        beanComDict.Catalog = "SALE_DOC_TYPE";
        lstCat = dao.FindByCatalog(beanComDict);
        bindDataToControl("lstDocType", lstCat);
    }


    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }

    // details click
    protected void viewSrhRst_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Request["x"] == null || Request["y"] == null)
            return;

        if (e.CommandName == "details")
        {                   
            int index = Convert.ToInt32(e.CommandArgument);
            // Get the Key value from the GridView control.
            DataKey key = viewSrhRst.DataKeys[index];
            string docId = key.Value.ToString();
          
            Context.Items["docId"] = docId;
            Context.Items["docType"] = this.hidDocType.Value;
            Context.Items["docTitle"] = this.hidDocTitle.Value;
            Context.Items["docContent"] = this.hidDocContent.Value;
            Context.Items["docIdSrh"] = this.hidDocId.Value;
            // 传递分页编号
            Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;
            // Server.Transfer("./DocContractUpdForm.aspx", true);
            ControllerUtils.Transfer(this.Context, "~/App/Master/DocContractUpdForm.aspx", true);
        }
    }

    // before render comlete
    void Page_PreRenderComplete(object sender, EventArgs e)
    {
        tblResult.Visible = true;
            if (viewSrhRst.Rows.Count < 1)
            {
                tblResult.Visible = false;
                if (string.IsNullOrEmpty(((Design_MasterPage)this.Master).InfoMessage)
                    && COMMAND_SEARCH.Equals(Convert.ToString(ViewState["PAGE_COMMAND"])))
                {
                    ((Design_MasterPage)this.Master).InfoMessage =
                        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0005").ToString();
                }
            }
    }

    // bind db data to selected controls
    private void bindDataToControl(string ControlId, IList LstData)
    {
        if (LstData == null || LstData.Count == 0) return;

        foreach (CommonDictionaryBean bean in LstData)
        {
            if (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is DropDownList)
            {
                ((DropDownList)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
            else if ((this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is CheckBoxList)
                || (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is RadioButtonList))
            {
                ((ListControl)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
        }
    }

}
